
package com.hlkj.pay.app.merchant.ext.impl.dffpay.utils;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.merchant.ext.dto.access.MerchantAccessCallBackDto;
import com.hlkj.pay.app.merchant.ext.dto.access.MerchantBusinessCallBackDto;
import com.hlkj.pay.app.merchant.ext.dto.access.MerchantChannelSplitCallBackDto;
import com.hlkj.pay.app.merchant.ext.impl.dffpay.dto.DffPayConfig;
import com.hlkj.pay.app.merchant.ext.impl.easypay.dto.EasyPayConfig;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAccessAuthDO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author HlpayTeam
 * @date 2024/10/09 10:02
 */
public class DffPayCallBackParamsParseUtil {

    /**
     * 进件回调参数解析
     *
     * @param callBackBody
     * @return
     */
    public static MerchantAccessCallBackDto accessCallBackParamsParse(String callBackBody, EasyPayConfig easyPayConfig) {
        MerchantAccessCallBackDto merchantAccessCallBackDto = new MerchantAccessCallBackDto();
        Map<String, String> map = JsonUtils.parseObject(callBackBody, Map.class);
        String mac = map.remove("MAC");
        // 参数组装
        String sign = DffPaySignUtil.md5Sign(map, easyPayConfig.getMd5Key());
        if (!mac.equals(sign)) {
            merchantAccessCallBackDto.setProcessStatus(false);
            merchantAccessCallBackDto.setRemark("验签失败");
            return merchantAccessCallBackDto;
        }
        if ("AGMERAPPLY".equals(map.get("recentMessType"))) {
            merchantAccessCallBackDto.setProcessStatus(false);
            merchantAccessCallBackDto.setRemark("方法调用错误");
            return merchantAccessCallBackDto;
        }
        // 参数组装
        String reqSeqId = map.get("operaTrace");
        merchantAccessCallBackDto.setRequestId(reqSeqId);
        String auditStatus = map.get("auditStatus");
        switch (auditStatus) {
            case "0":
                //成功
                merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.CHANNEL_APPROVE);
                break;
            case "5":
                //成功
                merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SIGN);
                break;
            case "X":
            case "1":
                //失败
                merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                break;
            default:
                //审核中
                merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.CHANNEL_APPROVE);
                break;
        }
        return merchantAccessCallBackDto;
    }

    /**
     * 进件回调参数解析
     *
     * @param easyPayConfig
     * @param callBackBody
     * @return
     */
    public static boolean verify(DffPayConfig easyPayConfig, String callBackBody) {
        // 参数组装
        JSONObject response = JSON.parseObject(callBackBody);
        return DffPaySignUtil.verify(response, easyPayConfig);
    }

    /**
     * 业务开通参数解析
     *
     * @param callBackBody
     * @return
     */
    public static MerchantBusinessCallBackDto businessCallBackParamsParse(String callBackBody, DffPayConfig easyPayConfig) {
        MerchantBusinessCallBackDto merchantBusinessCallBackDto = new MerchantBusinessCallBackDto();

        // 参数组装
        JSONObject response = JSON.parseObject(callBackBody);
        if (DffPaySignUtil.verify(response, easyPayConfig)) {
            merchantBusinessCallBackDto.setProcessStatus(false);
            merchantBusinessCallBackDto.setRemark("验签失败");
            return merchantBusinessCallBackDto;
        }
        JSONObject reqBody = response.getJSONObject("reqBody");
        // 参数组装
        String reqSeqId = reqBody.get("applyNo").toString();
        merchantBusinessCallBackDto.setRequestId(reqSeqId);
        String auditStatus = reqBody.get("applyNoState").toString();
        switch (auditStatus) {
            case "0":
                merchantBusinessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SIGN);
                break;
            case "1":
            case "9":
                merchantBusinessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.CHANNEL_APPROVE);
                break;
            case "X":
                merchantBusinessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                merchantBusinessCallBackDto.setRemark("易生审核失败,接口返回信息:" + reqBody.getString("remark"));
                break;
            default:
                merchantBusinessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                merchantBusinessCallBackDto.setRemark("易生审核失败,接口返回信息: 状态未知");
                break;
        }
        return merchantBusinessCallBackDto;
    }

    /**
     * 业务开通参数解析
     *
     * @param callBackBody
     * @return
     */
    public static MerchantChannelSplitCallBackDto easySplitCallBackParamsParse(String callBackBody, EasyPayConfig easyPayConfig) {
        MerchantChannelSplitCallBackDto merchantChannelSplitCallBackDto = new MerchantChannelSplitCallBackDto();

        Map<String, String> map = JsonUtils.parseObject(callBackBody, Map.class);
        String mac = map.remove("MAC");
        // 参数组装
        String sign = DffPaySignUtil.md5Sign(map, easyPayConfig.getMd5Key());
        if (!"SENDZFBORWX".equals(map.get("recentMessType"))) {
            merchantChannelSplitCallBackDto.setProcessStatus(false);
            merchantChannelSplitCallBackDto.setRemark("方法调用错误");
            return merchantChannelSplitCallBackDto;
        }
        if (!mac.equals(sign)) {
            merchantChannelSplitCallBackDto.setProcessStatus(false);
            merchantChannelSplitCallBackDto.setRemark("验签失败");
            return merchantChannelSplitCallBackDto;
        }
        String auditStatus = map.get("auditStatus");
        List<MerchantAccessAuthDO> merchantAccessAuthDOList = new ArrayList<>();
        switch (auditStatus) {
            case "0":
                String funcInfo = map.get("funcInfo");
                JSONArray funcObj = JSON.parseArray(funcInfo);
                for (int i = 0; i <funcObj.size(); i++) {
                    MerchantAccessAuthDO merchantAccessAuthDO = new MerchantAccessAuthDO();
                    JSONObject obj = funcObj.getJSONObject(i);
                    if ("2".equals(obj.getString("funcId"))) {
                        merchantAccessAuthDO.setType(2);
                        merchantAccessAuthDO.setStatus(obj.getInteger("state"));
                        merchantAccessAuthDO.setSubMchId(obj.getString("alipayId"));
                        merchantAccessAuthDOList.add(merchantAccessAuthDO);
                    } else if ("3".equals(obj.getString("funcId"))) {
                        merchantAccessAuthDO.setType(1);
                        merchantAccessAuthDO.setStatus(obj.getInteger("state"));
                        merchantAccessAuthDO.setSubMchId(obj.getString("wechatId"));
                        merchantAccessAuthDOList.add(merchantAccessAuthDO);
                    }
                }
                break;
        }
        merchantChannelSplitCallBackDto.setMerchantAccessAuthDOList(merchantAccessAuthDOList);
        return merchantChannelSplitCallBackDto;
    }
}
